package org.mobiletrain.www.secondproject2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import adapter.CheckPlaceAdapter;
import bean.CheckPlace;


public class CheckPlaceActivity extends ActionBarActivity {

    private static final String TAG ="CheckPlaceActivity" ;
    private ArrayList<CheckPlace> places;
    private CheckPlaceAdapter adapter;
    private String path="http://appapi.yaochufa.com/v2/Position/GetCityList?machineCode=865026028677325&version=4.2.2&system=android&channel=yaochufa";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_place);

        Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar2);
        toolBar.setTitle("选择城市");
        getSupportActionBar().hide();
        places = new ArrayList<CheckPlace>();
        ListView checkPlaceList = (ListView) findViewById(R.id.checkpalce);
        ProgressBar barPlace = (ProgressBar) findViewById(R.id.progressBar_place);
        checkPlaceList.setEmptyView(barPlace);
        getPlaceData();
        adapter = new CheckPlaceAdapter(places,CheckPlaceActivity.this);
        checkPlaceList.setAdapter(adapter);

        checkPlaceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityCode = places.get(position).getCityCode();
                String cityId = places.get(position).getCityId();
                String cityName = places.get(position).getCityName();
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("cityCode",cityCode);
                bundle.putString("cityName",cityName);
                bundle.putString("cityId",cityId);
                intent.putExtra("bundle",bundle);
                intent.setClass(CheckPlaceActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });


    }

    private void getPlaceData() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String json = objectResponseInfo.result.toString();
                Log.i(TAG, "==========" + json);
                try {
                    JSONObject object = new JSONObject(json);
                    JSONObject data = object.getJSONObject("data");
                    JSONArray hotCity = data.getJSONArray("positionCity");
                    for (int i = 0; i <hotCity.length() ; i++) {
                        JSONObject jsonObject = hotCity.getJSONObject(i);
                        String cityId = jsonObject.getString("cityId");
                        String cityCode = jsonObject.getString("cityCode");
                        String pinYinName = jsonObject.getString("pinYinName");
                        String cityName = jsonObject.getString("cityName");
                        String cityNameAbbr = jsonObject.getString("cityNameAbbr");

                        CheckPlace checkPlace =
                                new CheckPlace(cityId, cityCode, pinYinName, cityName, cityNameAbbr);
                        places.add(checkPlace);
                    }

                    adapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(CheckPlaceActivity.this,"下载失败",Toast.LENGTH_SHORT).show();

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_check_place, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
